﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WCFMINERP.BLL.PhotoBll;
using WCFMINERP.BLL.PhotoDownload;
using WCFMINERP.MODEL;
using WCFMINERP.ComControls;


namespace WCFMINERP.FileHosting
{
    public partial class frmSelectFile : Form
    {
        public frmSelectFile()
        {
            InitializeComponent();
        }

        PhotoinfoBll photoBll = new PhotoinfoBll();
        PhotoDownloadBll loadBll = new PhotoDownloadBll();
        string serverFile = "";

        /// <summary>
        /// 加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmSelectFile_Load(object sender, EventArgs e)
        {
            string strPath = photoBll.GetServerPhoto();  //获取已经开单的路径
            if (string.IsNullOrEmpty(HelpClass.SaveFilePath))
            {
                HelpClass.SaveFilePath = strPath;
            }
            else if (strPath != HelpClass.SaveFilePath)
            {
                HelpClass.SaveFilePath = strPath;
            }

            serverFile = HelpClass.SaveFilePath + "\\Folder\\";

            //不存在创建目录
            if (!System.IO.Directory.Exists(serverFile))
            {
                System.IO.Directory.CreateDirectory(serverFile);
            }

            List<OutShop>  oupshoplist = loadBll.GetShopList();//获取工厂集合
            com_Box.FillCollections(oupshoplist, p => p.shopName, p => p);
        }

        List<string> list = new List<string>();

        /// <summary>
        /// 选择文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void but_SelectFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Multiselect = true;
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                foreach (string s in fileDialog.FileNames)
                {
                    if (!list.Contains(s))
                    {
                        list.Add(s);
                    }
                }
                listView1.Items.Clear();
                foreach (string s in list)
                {
                    ListViewItem item = new ListViewItem(new string[] { s, serverFile + System.IO.Path.GetFileName(s) });
                    listView1.Items.Add(item);
                }
            }
        }

        /// <summary>
        /// 移除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void but_Remove_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listView1.SelectedItems)
            {
                if (list.Contains(item.SubItems[0].Text))
                {
                    list.Remove(item.SubItems[0].Text);
                }
                listView1.Items.Remove(item);
            }
        }

        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void but_Determine_Click(object sender, EventArgs e)
        {
            List<OutShop> dataList = com_Box.GetCheckedObjectItemOfType<OutShop>().ToList();

            bool b = true;
            foreach (ListViewItem item in listView1.Items)
            {
                File.Copy(item.SubItems[0].Text, item.SubItems[1].Text, true);  //复制文件

                //添加数据
                if (!loadBll.AddDownLoad(dataList, item.SubItems[1].Text))
                {
                    b = false;
                    break;
                }
            }
            if (!b)
            {
                lbl_Mess.Text = "操作失败！";
                //DialogResult = System.Windows.Forms.DialogResult.No;
            }
            else
            {
                lbl_Mess.Text = "操作成功！";
                listView1.Items.Clear();
                list.Clear();
               // DialogResult = System.Windows.Forms.DialogResult.OK;
            }
        }
    }
}
